using System.IO;
using System.Web;
using System.Web.Mvc;
using CloudinaryDotNet.Actions;
using OurWedding1.Models;

namespace OurWedding1.Controllers
{
    [Authorize]
    public class WeddingPhotosController : BaseController
    {
        private readonly IWeddingRepository _weddingRepository;
        private readonly IWeddingPhotoRepository _weddingphotoRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public WeddingPhotosController()
            : this(new WeddingRepository(), new WeddingPhotoRepository())
        {
        }

        public WeddingPhotosController(IWeddingRepository weddingRepository, IWeddingPhotoRepository weddingphotoRepository)
        {
            _weddingRepository = weddingRepository;
            _weddingphotoRepository = weddingphotoRepository;
        }

        //
        // GET: /WeddingPhotoes/

        public ViewResult Index()
        {
            return View(_weddingphotoRepository.AllIncluding(weddingphoto => weddingphoto.Wedding));
        }

        //
        // GET: /WeddingPhotoes/Details/5

        public ViewResult Details(int id)
        {
            return View(_weddingphotoRepository.Find(id));
        }

        public ActionResult Upload()
        {
            var selectedWedding = SelectedWeddingId;
            if (!selectedWedding.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            var selectedWedding = SelectedWeddingId;
            if (!selectedWedding.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            if (file.ContentLength > 0)
            {
                //var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                //file.SaveAs(path);
                var account = new CloudinaryDotNet.Account("hdq73umpz", "694358231294957", "U2rCpJXbmiH2dj8gUxQ2bhI7lFg");

                var cloudinary = new CloudinaryDotNet.Cloudinary(account);
                var uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams
                    {
                        File = new CloudinaryDotNet.Actions.FileDescription("dama", file.InputStream)
                    };

                CloudinaryDotNet.Actions.ImageUploadResult uploadResult = cloudinary.Upload(uploadParams);
                var model = new WeddingPhoto {WeddingId = selectedWedding.Value, PublicId = uploadResult.PublicId};
                _weddingphotoRepository.InsertOrUpdate(model);
                _weddingphotoRepository.Save();
                return RedirectToAction("Details", new {id = model.WeddingPhotoId});
            }

            return RedirectToAction("Index");
        }


        //
        // GET: /WeddingPhotoes/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_weddingphotoRepository.Find(id));
        }

        //
        // POST: /WeddingPhotoes/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _weddingphotoRepository.Delete(id);
            _weddingphotoRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _weddingRepository.Dispose();
                _weddingphotoRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

